//
//  HOQuestionListViewController.m
//  微度报修
//
//  Created by foguo-mac-1 on 08/05/2017.
//  Copyright © 2017 WeiDu. All rights reserved.
//

#import "HOQuestionListViewController.h"
#import "HORefreshViewController.h"
#import "HOConstants.h"
#import "HOQuestionEntity.h"
#import "HOUtils.h"
#import "HOQuestionDetailViewController.h"
#import "HOAddQuestionViewController.h"

@interface HOQuestionListViewController ()
{
	NSMutableDictionary *questionsDic;
}

@end

@implementation HOQuestionListViewController

- (void)viewDidLoad {
    [super viewDidLoad];
	// Do any additional setup after loading the view.
	[self initTableViewWithPullUp];
	[self initNoDataView];
	[self initData];
}

- (void)initData
{
	questionsDic = [[NSMutableDictionary alloc] init];
}

- (void)dealloc
{
	[questionsDic removeAllObjects];
	[questionsDic release];
	[super dealloc];
}

- (void)viewWillAppear:(BOOL)animated
{
	[super viewWillAppear:animated];
	self.tabBarController.tabBar.hidden = YES;
	[self setNavigationItemTitle:NSLocalizedString(@"问题列表", @"问题列表")];
	[self requestDataWithMaxId:0];
	[self displayRightButtomItemWithTitle:NSLocalizedString(@"提问", @"提问")];
}

- (void)rightButtonItemDidClick
{
	HOAddQuestionViewController *viewCtrl = [[HOAddQuestionViewController alloc] init];
	[self.navigationController pushViewController:viewCtrl animated:YES];
	[viewCtrl release];
}

- (void)requestDataWithMaxId:(int)maxId
{
	[self sendRequestWithParams:@{kHO_FIELD_Action:kHO_ACTION_GetQuestionList,
								  kHO_FIELD_MaxID:[NSNumber numberWithInt:maxId]}];
}


- (void)pullUpToRefresh
{
	HOQuestionEntity *entity = [self.tableViewDataList lastObject];
	if(entity)
	{
		[self requestDataWithMaxId:entity.questionId];
	}
	else
	{
		[self requestDataWithMaxId:1];
	}
}


#pragma mark - -- UITableViewDelegate/UITableViewDataResource implement methods
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
	HOQuestionEntity *entity = [self.tableViewDataList objectAtIndex:indexPath.row];
	UILabel *titleLbl = [[[UILabel alloc]
							 initWithFrame:CGRectMake(HO_HORIZONAL_PADDING,
													  8,
													  tableView.frame.size.width - 40,
													  100)]autorelease];
	titleLbl.numberOfLines = 0;
	titleLbl.text = [NSString stringWithFormat:@"%@：%@", NSLocalizedString(@"提问", @"提问"), entity.questionTitle];
	[titleLbl sizeToFit];
	return 16 + 25 + titleLbl.frame.size.height;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
	return 1;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
	NSInteger count = [self.tableViewDataList count];
	[self updateNoResultView:count];;
	return count;
}


- (CGFloat) tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
//	if(section == 0)
//		return [HOUtils defaultOnePixelConversion];
	return SECTION_PADDING;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
	static NSString *HOTableViewCell = @"HOTableViewCell";
	UITableViewCell *cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:HOTableViewCell];
	if(cell == nil)
	{
		cell = [[[UITableViewCell alloc]
				 initWithStyle:UITableViewCellStyleDefault
				 reuseIdentifier: HOTableViewCell] autorelease];
		cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
	}
	HOQuestionEntity *entity = [self.tableViewDataList objectAtIndex:indexPath.row];
	UILabel *titleLbl = (UILabel *)[cell viewWithTag:115];
	if(titleLbl == nil)
	{
		titleLbl = [[UILabel alloc]
					initWithFrame:CGRectMake(HO_HORIZONAL_PADDING,
											 8,
											 tableView.frame.size.width - 40,
											 100)];
		titleLbl.tag = 115;
		[cell.contentView addSubview:titleLbl];
		titleLbl.textColor = TEXT_COLOR_666666;
		titleLbl.font = [UIFont systemFontOfSize:15];
//		titleLbl.layer.borderWidth = 1;
		titleLbl.numberOfLines = 0;
		[titleLbl release];
	}
	titleLbl = (UILabel *)[cell viewWithTag:115];
	titleLbl.text = [NSString stringWithFormat:@"%@：%@", NSLocalizedString(@"提问", @"提问"), entity.questionTitle];
	[titleLbl sizeToFit];
	
	
	
	UILabel *timeLbl = (UILabel *)[cell viewWithTag:116];
	if(timeLbl == nil)
	{
		timeLbl = [[UILabel alloc]
				   initWithFrame:CGRectMake(baseTableview.frame.size.width - 140,
											titleLbl.frame.origin.y,
											110,
											22)];
		timeLbl.tag = 116;
//		timeLbl.textAlignment = NSTextAlignmentRight;
		[cell.contentView addSubview:timeLbl];
		timeLbl.textColor = TEXT_COLOR_999999;
		timeLbl.font = [UIFont systemFontOfSize:13];
//		timeLbl.layer.borderWidth = 1;
		[timeLbl release];
	}
	timeLbl = (UILabel *)[cell viewWithTag:116];
	timeLbl.frame = CGRectMake(HO_HORIZONAL_PADDING,
							   titleLbl.frame.origin.y + titleLbl.frame.size.height + 8,
							   110,
							   22);
	timeLbl.text = [HOUtils getDateFromTimeInterval:entity.createTime * 1000];
	return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
	[tableView deselectRowAtIndexPath:indexPath animated:YES];
	HOQuestionDetailViewController *viewCtrl = [[HOQuestionDetailViewController alloc] init];
	HOQuestionEntity *entity = [self.tableViewDataList objectAtIndex:indexPath.row];
	viewCtrl.questionEntity = entity;
	[self.navigationController pushViewController:viewCtrl animated:YES];
	[viewCtrl release];
	
}
#pragma mark

- (HOEditTableViewCell *)tableView:(UITableView *)tableView cellForEditRowAtIndexPath:(NSIndexPath *)indexPath
{
	static NSString *AMBaseTableViewCellIdentifier = @"HOTableViewCell";
	HOEditTableViewCell *cell = (HOEditTableViewCell *)[tableView dequeueReusableCellWithIdentifier:AMBaseTableViewCellIdentifier];
	if(cell == nil)
	{
		cell = [[[HOEditTableViewCell alloc]
				 initWithStyle:UITableViewCellStyleDefault
				 reuseIdentifier: AMBaseTableViewCellIdentifier] autorelease];
	}
	return cell;
}
#pragma mark -- tool
- (void)didReceiveMemoryWarning {
	[super didReceiveMemoryWarning];
	// Dispose of any resources that can be recreated.
}


- (void)requestDidSuccessWithResponse:(HOBaseResponseEntity *)response
{
	if([response isEqualAction:kHO_ACTION_GetQuestionList])
	{
		for(int i = 0; i < [response.dataNode count]; i ++)
		{
			NSDictionary *dic = [response.dataNode objectAtIndex:i];
			int questionId = [[dic objectForKey:kHO_FIELD_QuestionId] intValue];
			
			HOQuestionEntity *entity = [questionsDic objectForKey:[NSNumber numberWithInt:questionId]];
			if(entity == nil)
			{
				entity = [[HOQuestionEntity alloc] init];
				[entity parseDic:dic];
				[self.tableViewDataList addObject:entity];
				[questionsDic setObject:entity forKey:[NSNumber numberWithInt:questionId]];
				[entity release];
			}
		}
		[self doneLoadingTableViewRefresh];
	}
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
